let store = require('../utils/store')
let system = store.getSystemInfo()
const clientInfo = {
    'clentType': 'mp',
    'appnm': 'mingdu-mp',
    'model': system.model,
    'os': system.system,
    'screen': system.screenWidth + '*' + system.screenHeight,
    'version': App.version,
    'channel': 'miniprogram'
}

const errMsg = '服务异常，请稍后重试'
module.exports = {
    fetch: (url, data = {}, option = {}) => {
        let {
            loading = true, toast = false, isMock = false, method = 'get'
        } = option
        return new Promise((resolve, reject) => {
            if (loading) {
                wx.showLoading({
                    title: '加载中',
                    mask: true
                })
            }
            // 根路径（根据环境变量来切换）
            let env = isMock ? App.config.mockApi : App.config.clientApi
            wx.request({
                url: env + url,
                data,
                method,
                header: {
                    'clientInfo': JSON.stringify(clientInfo),
                    'Authorization': 'driver',
                    'phoneNumber': store.getItem("phoneNumber")
                },
                success: function (res) {
                    if (!res.data) {
                        if (loading) {
                            wx.hideLoading()
                        }
                        wx.showToast({
                            mask: true,
                            title: '抱歉，没有获取到数据',
                            icon: 'none'
                        })
                        reject(res)
                    }
                    if (res.data.code === 0) {
                        if (loading) {
                            wx.hideLoading()
                        }
                        if (res.data.result) {
                            resolve(res.data.result)
                        } else if (res.data.data) {
                            resolve(res.data.data)
                        }

                    } else {
                        if (toast) {
                            wx.showToast({
                                mask: true,
                                title: res.message,
                                icon: 'none'
                            })
                        } else {
                            wx.hideLoading()
                        }
                        reject(res)
                    }
                },
                fail: function (e = {
                    code: -1,
                    msg: errMsg,
                    errMsg
                }) {
                    console.log('错误事件e', e)
                    let msg = e.errMsg
                    if (msg === 'request:fail timeout') {
                        msg = '服务器请求超时，请稍后处理'
                    }
                    wx.showToast({
                        title: msg,
                        icon: 'none'
                    })
                    reject(e)
                }
            })
        })
    }
}